﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaytracerLib.Layers;
using RaytracerLib.Volumes;
using RaytracerLib.MathLib;

namespace RaytracerLib.Materials
{
    public static class MaterialOptimizer
    {
        public static void Optimize(Material a_material)
        {
            if (a_material.Materials.Scene.RenderOptions.AmbientColor == ColorFloat.Black)
                a_material.Ambient = null;

            foreach (var part in a_material.MaterialParts)
            {
                if (IsZero(part))
                    a_material.ReplaceMaterialPart(part, null);
            }
        }

        private static bool IsZero(IMaterialPart a_material_part)
        {
            if (a_material_part is SolidColorLayer)
                return (a_material_part as SolidColorLayer).Color == ColorFloat.Black;
            else if (a_material_part is SolidColorVolume)
                return (a_material_part as SolidColorVolume).Color == ColorFloat.Black;
            else if (a_material_part is SolidValueLayer)
                return (a_material_part as SolidValueLayer).Value == 0;
            else if (a_material_part is SolidValueVolume)
                return (a_material_part as SolidValueVolume).Value == 0;

            return false;
        }


    }
}
